package com.cn.jsdr.service.serviceImpl;

import com.cn.jsdr.service.RediService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class RediServiceImpl implements RediService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Map<String, Object> getSql(String sqlId) {
        Map<String, Object> m  =  redisTemplate.opsForHash().entries(sqlId);
        //Map<String, Object> m  =  null;
        if(m== null||m.size()==0){
            String sql = "select * from sqlconf where sqlid ="+ sqlId;
            m= jdbcTemplate.queryForMap(sql);
            redisTemplate.opsForHash().putAll(sqlId, m);
        }
        return m;
    }
    //刷新redis缓存的sql
    @Override
    public Map<String, Object> refreshSql(String sqlId) {
        String sql = "select * from sqlconf where sqlid ="+ sqlId;
        Map<String, Object> m= jdbcTemplate.queryForMap(sql);
        redisTemplate.opsForHash().putAll(sqlId, m);
        return m;
    }
}
